package ex.com.jm.food.dao.impl;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.filefilter.IOFileFilter;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.transform.Transformers;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import ex.com.jm.food.dao.UserMenuDao;
import ex.com.jm.food.entity.Menu;

public class UserMenuDaoImpl extends HibernateDaoSupport implements UserMenuDao {

	public UserMenuDaoImpl() {
		// TODO Auto-generated constructor stub
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<Menu> getMenus(final int initPage,final int pageRow,final String comId,
			final String order,final String sort) {
		// TODO Auto-generated method stub
		Object menus=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String sql="select  "
						+ " menu.menu_name as menuName,"
						+ " menu.menu_id as menuId,"
						+ " menu.menu_nowPrice as menuPrice,"
						+ " menu.menu_image as menuImage,"
						+ " mps.avg_eval as avgEval,"
						+ " mps.total_num as totalNum"
						+ " from jm_menu as menu "
						+ " left join jm_menu_profit_summary as mps on menu.menu_id=mps.menu_id "
						+ " where menu.com_id='"+comId+"'";
				if(order!=null&&!order.equals("")&&sort!=null&&!sort.equals("")){
					sql+=" order by "+order+" "+sort;
				}
				Query query=session.createSQLQuery(sql).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).setFirstResult(initPage).setMaxResults(pageRow);
				return query.list();
			}
		});
		return (List<Menu>) menus;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public int menuCount(final String comId) {
		// TODO Auto-generated method stub
		int count=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql="from Menu where comId='"+comId+"'";
				Query query=session.createQuery(hql);
				return query.list().size();
			}
		});
		return count;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public  List<Map<String, String>> getCompany(final String comId) {
		// TODO Auto-generated method stub
		Object company=this.getHibernateTemplate().execute(new HibernateCallback() {

			@SuppressWarnings("deprecation")
			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String sql="select "
						+ "com.com_id as comId,"
						+ "com.com_name as comName,"
						+ "com.com_image as comImage,"
						+ "com.com_distr as comDis,"
						+ "cps.total_num as totalNum,"
						+ "cps.avg_eval as avgEval,"
						+ "cps.room_date as roomDate,"
						+ "notice.notice_content as content,"
						+ "ct.comtype_name as comtypeName"
						+ " from jm_company as com "
						+ " left join jm_company_profit_summary as cps on com.com_id=cps.com_id"
						+ " left join jm_notice as notice on com.com_id=notice.com_id  "
						+ " left join jm_companytype ct on com.comtype_id=ct.comtype_id "
						+ " where com.com_id='"+comId+"'";
				Query query=session.createSQLQuery(sql).addScalar("comId", Hibernate.STRING).addScalar("comName",Hibernate.STRING).addScalar("comImage",Hibernate.STRING)
						.addScalar("comDis",Hibernate.STRING).addScalar("totalNum",Hibernate.INTEGER).addScalar("avgEval",Hibernate.INTEGER).addScalar("comtypeName", Hibernate.STRING)
						.addScalar("roomDate", Hibernate.INTEGER).addScalar("content", Hibernate.STRING).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
				return query.list();
			}
		});
		return (List<Map<String, String>>) company;
	}

	@Override
	public Menu getMenu(String menuId) {
		// TODO Auto-generated method stub
		return this.getHibernateTemplate().get(Menu.class, menuId);
	}

}
